import { computed } from "vue";
import { message as Message } from "ant-design-vue";
import { updateTrackStatusApi, syncTrackApi } from "@/api/waybillManagement/customerServiceWaybill";
export default function TrackInfoHook(state: { tableRef: any; searchHandle: () => void; getData: () => void }) {
  const { searchHandle, tableRef, getData } = state;
  const selectRows = computed<Array<ClientWaybillManagement.ListRows>>(() => {
    return tableRef.value.xTable.getCheckboxRecords() || [];
  });
  //运踪屏蔽
  const trackInfoHideHandle = (num: number) => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");

    return updateTrackStatusApi({ idList: selectRows.value.map((t: any) => t.id), trackStatus: num }).then((res: any) => {
      const { code } = res;
      if (code === 200) {
        Message.success("操作成功!");
        searchHandle();
      }
    });
  };

  //更新运踪
  const updateTrackHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    if (selectRows.value.length > 100) return Message.error("一次勾选最多100条!");

    return syncTrackApi({ idList: selectRows.value.map((t: any) => t.id) }).then((res: any) => {
      console.log(res);
      const { code, data } = res;
      if (code === 200) {
        Message.success(data);
        getData();
      }
    });
  };
  return { trackInfoHideHandle, updateTrackHandle };
}
